package org.esti.controller;

import org.esti.model.User;
import org.esti.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;


/**
 * @author Artem Loginov (logart)
 */

/**
 * This controller is used for providing mapping between request and view
 * and showing profile information.
 * For example: forms for view and editing profile
 */
@Controller
@RequestMapping(value = "/{login}/profile")
public class ProfileController {

    @Autowired
    private UserService userService;

    /**
     * Show edit user profile form
     *
     * @param login      of user whose profile we will edit
     * @param firstLogin flag of help information. If set to true user will see some additional "first step" information.
     * @param model      set of parameters which will be passed to view. In this case user fields(name, birthday, etc.)
     * @return name of view which will be render
     */
    @RequestMapping(params = {"action=edit"}, method = RequestMethod.GET)
    public String showEditProfileForm(
            @PathVariable String login,
            @RequestParam(value = "first_login", required = false) Boolean firstLogin,
            Model model) {

        model.addAttribute("first_login", firstLogin);

        User user = userService.getUserByLogin(login);
        model.addAttribute("user", user);

        return "edit_profile";
    }

    /**
     * Update user in database using information from parameters
     *
     * @param name       new user name
     * @param middleName new user middle name
     * @param surname    new user surname
     * @param birthday   new user date of birthday
     * @param login      of user whose account data will be updated
     * @param model      set of parameters which will be passed to view. In this case user fields(name, birthday, etc.)
     * @return name of view which will be render
     */
    @RequestMapping(params = {"action=edit"}, method = RequestMethod.POST)
    public String processEditProfileForm(
            @RequestParam String name,
            @RequestParam String middleName,
            @RequestParam String surname,
            @RequestParam Date birthday,
            @PathVariable String login,
            Model model) {

        User user = userService.updateUser(
                login,
                name,
                middleName,
                surname,
                birthday);

        model.addAttribute("user", user);

        return "redirect:/" + user.getLogin() + "/profile?action=edit";
    }

    /**
     * Shows user profile
     *
     * @param login of user whose profile we want to show
     * @param model set of parameters which will be passed to view. Fro example user and his appraisal.
     * @return name of view which will be render
     */
    @RequestMapping()
    public String showProfile(@PathVariable String login, Model model) {

        User user = userService.getUserByLogin(login);
        model.addAttribute("user", user);

        return "view_profile";
    }

    /**
     * Show change avatar form.
     * This form is used to upload new photo of user.
     *
     * @return name of view which will be render
     */
    @RequestMapping(params = {"action=change_avatar"})
    public String showChangeAvatarForm() {
        return "edit_avatar";
    }
}
